package org.yray.sip.time;


import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.RemovalListener;
import lombok.extern.slf4j.Slf4j;
import org.yray.sip.log.AbstractLog;

import java.util.concurrent.TimeUnit;

@Slf4j
public class StopSendRtpTimer {

    private static Cache<String,Object> timerCache= Caffeine.newBuilder()
            // 数量上限
            .maximumSize(2048)
            // 过期机制,表示自从最后一次写入后多久就会过期；
            //expireAfterAccess：表示自从最后一次访问（写入或者读取）后多久就会过期；expireAfter：自定义过期策略；
            .expireAfterWrite(60, TimeUnit.SECONDS)
            // 剔除监听
            .removalListener((RemovalListener<String, Object>) (key, value, cause) ->
                    AbstractLog.log(key ,"StopSendRtpTimer" , " 删除原因:{}" , cause.toString()))
            .build();

    public static void cleanTimer(String key,int seq)
    {
        AbstractLog.log(key ,"StopSendRtpTimer","cleanTimer,seq={}",seq);
        timerCache.put(key+seq,new Object());
        timerCache.put(key,new Object());
    }

    public static Object ifClean(String key)
    {
        return timerCache.getIfPresent(key);
    }
}
